package com.ruoyi.gis.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gis.componets.RasterDatasetComponent;
import com.ruoyi.gis.domain.DatasetRaster;
import com.ruoyi.gis.service.IDatasetRasterService;
import com.ruoyi.gis.vo.CollectAddImageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gis.mapper.DatasetRasterCollectedMapper;
import com.ruoyi.gis.domain.DatasetRasterCollected;
import com.ruoyi.gis.service.IDatasetRasterCollectedService;

/**
 * 用户收藏的数据集Service业务层处理
 *
 * @author rww
 * @date 2024-08-30
 */
@Service
public class DatasetRasterCollectedServiceImpl implements IDatasetRasterCollectedService
{
    @Autowired
    private DatasetRasterCollectedMapper datasetRasterCollectedMapper;
    @Autowired
    private IDatasetRasterService datasetRasterService;
    @Autowired
    private RasterDatasetComponent rasterDatasetComponent;
    /**
     * 查询用户收藏的数据集
     *
     * @param id 用户收藏的数据集主键
     * @return 用户收藏的数据集
     */
    @Override
    public DatasetRasterCollected selectDatasetRasterCollectedById(String id)
    {
        return datasetRasterCollectedMapper.selectDatasetRasterCollectedById(id);
    }

    /**
     * 查询用户收藏的数据集列表
     *
     * @param datasetRasterCollected 用户收藏的数据集
     * @return 用户收藏的数据集
     */
    @Override
    public List<DatasetRasterCollected> selectDatasetRasterCollectedList(DatasetRasterCollected datasetRasterCollected)
    {
        return datasetRasterCollectedMapper.selectDatasetRasterCollectedList(datasetRasterCollected);
    }

    /**
     * 新增用户收藏的数据集
     *
     * @param datasetRasterCollected 用户收藏的数据集
     * @return 结果
     */
    @Override
    public int insertDatasetRasterCollected(DatasetRasterCollected datasetRasterCollected)
    {
        datasetRasterCollected.setId(IdUtil.getSnowflakeNextIdStr());
        datasetRasterCollected.setCreateTime(DateUtils.getNowDate());
        return datasetRasterCollectedMapper.insertDatasetRasterCollected(datasetRasterCollected);
    }

    /**
     * 修改用户收藏的数据集
     *
     * @param datasetRasterCollected 用户收藏的数据集
     * @return 结果
     */
    @Override
    public int updateDatasetRasterCollected(DatasetRasterCollected datasetRasterCollected)
    {
        datasetRasterCollected.setUpdateTime(DateUtils.getNowDate());
        return datasetRasterCollectedMapper.updateDatasetRasterCollected(datasetRasterCollected);
    }

    /**
     * 批量删除用户收藏的数据集
     *
     * @param ids 需要删除的用户收藏的数据集主键
     * @return 结果
     */
    @Override
    public int deleteDatasetRasterCollectedByIds(String[] ids)
    {
        return datasetRasterCollectedMapper.deleteDatasetRasterCollectedByIds(ids);
    }

    /**
     * 删除用户收藏的数据集信息
     *
     * @param id 用户收藏的数据集主键
     * @return 结果
     */
    @Override
    public int deleteDatasetRasterCollectedById(String id)
    {
        return datasetRasterCollectedMapper.deleteDatasetRasterCollectedById(id);
    }

    @Override
    public DatasetRasterCollected selectUserCollect(String dataId, Long userId) {
        return datasetRasterCollectedMapper.selectUserCollect(dataId, userId);
    }

    @Override
    public DatasetRasterCollected addFolder(Long userId, String pid, String name) {
        DatasetRasterCollected parentFile = null;
        if("0".equalsIgnoreCase(pid)){
            parentFile = new DatasetRasterCollected();
            parentFile.setId("0");
        }else {
            parentFile = datasetRasterCollectedMapper.selectDatasetRasterCollectedById(pid);
        }
        if (parentFile != null) {
            DatasetRasterCollected query = datasetRasterCollectedMapper.
                    selectByUserIdAndPidAndNameAndType(userId,
                    pid, name, DatasetRasterCollected.type_folder);
            if (query != null) return query;
            DatasetRasterCollected folder = new DatasetRasterCollected();
            folder.setId(IdUtil.getSnowflakeNextIdStr());
            folder.setType(DatasetRasterCollected.type_folder);
            folder.setName(name);
            folder.setPid(pid);
            folder.setUserId(userId);
            Date date = new Date();
            folder.setCreateTime(date);
            datasetRasterCollectedMapper.insertDatasetRasterCollected(folder);
            //同时新增layerGroup
//            gisLayerService.insertGisLayer()
            return folder;
        } else {
            return null;
        }
    }

    @Override
    public void addCollectImages(LoginUser loginUser, CollectAddImageDto dto) {
//        String pid = StringUtils.isEmpty(dto.getPid())?"0":dto.getPid();
//        dto.getDataIds().stream().forEach(dataId->{
//            DatasetRaster raster = datasetRasterService.selectDatasetRasterByDataId(dataId);
//            if(raster!=null){
//                DatasetRasterCollected collected = new DatasetRasterCollected();
//                collected.setId(IdUtil.getSnowflakeNextIdStr());
//                collected.setType(DatasetRasterCollected.type_file);
//                collected.setName(raster.getName());
//                collected.setPid(pid);
//                collected.setUserId(loginUser.getUserId());
//                collected.setRasterId(raster.getId());
//                collected.setDataId(dataId);
//                collected.setSourceType(raster.getSourceType());
//                Date date = new Date();
//                collected.setCreateTime(date);
//                datasetRasterCollectedMapper.insertDatasetRasterCollected(collected);
//                if(DatasetRaster.state_undownload.
//                        equals(raster.getFileDownloadState())){
//                    //发送下载请求
//                    rasterDatasetComponent.sendDownloadDataIdRequest(dataId);
//                }
//            }
//        });
    }
}
